/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.arg.deploy.webapp.common;

import com.arg.deploy.bean.Tag;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 *
 * @author utaino
 */
public class ImportFileReaderUtil {

    private static Log LOG = LogFactory.getLog(ImportFileReaderUtil.class);

    public static List<String[]> readXlsExcelDataList(InputStream inputStream, int totalColumn, boolean hasHeader) throws Exception {

        List<String[]> dataList = new ArrayList<String[]>();
        try {
            Workbook workbook = WorkbookFactory.create(inputStream);
            Sheet sheet = workbook.getSheetAt(0);
            Iterator<Row> rowIterator = sheet.iterator();

            Iterator<Row> rowIteratorCheck = sheet.iterator();
            Row rowCheck = rowIteratorCheck.next();
            Iterator<Cell> cellIterator = rowCheck.cellIterator();
            int idx = 0;
            while (cellIterator.hasNext()) {
                cellIterator.next();
                idx++;
            }
            if (idx != totalColumn) {
                return null;
            } else {
                if (hasHeader) {
                    if (rowIterator.hasNext()) {
                        rowIterator.next();
                    }
                }
                while (rowIterator.hasNext()) {
                    Row row = rowIterator.next();
                    List<String> cellDataList = new ArrayList<String>();
                    for (int i = 0; i < totalColumn; i++) {
                        Cell cell = row.getCell(i);
                        if (cell == null) {
                            cellDataList.add("");
                        } else {
                            cell.setCellType(Cell.CELL_TYPE_STRING);
                            cellDataList.add(cell.getStringCellValue());
                        }
                    }
                    String[] cellArray = new String[totalColumn];
                    for (int j = 0; j < cellDataList.size(); j++) {
                        cellArray[j] = cellDataList.get(j);
                    }
                    dataList.add(cellArray);
                }
            }
        } catch (Exception ex) {
            throw ex;
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    throw e;
                }
            }
        }

        return dataList;

    }

    public static List<String[]> readXlsxExcelDataList(InputStream inputStream, int totalColumn, boolean hasHeader) throws Exception {

        LOG.info(":::::::::::; inputStream = " + inputStream);
        LOG.info(":::::::::::; totalColumn = " + totalColumn);
        LOG.info(":::::::::::; hasHeader = " + hasHeader);

        List<String[]> dataList = new ArrayList<String[]>();
        try {
            XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
            XSSFSheet sheet = workbook.getSheetAt(0);
            Iterator<Row> rowIterator = sheet.iterator();

            LOG.info(":::::::::::; rowIterator = " + rowIterator);

            Iterator<Row> rowIteratorCheck = sheet.iterator();
            Row rowCheck = rowIteratorCheck.next();
            Iterator<Cell> cellIterator = rowCheck.cellIterator();

            LOG.info(":::::::::::; rowIteratorCheck = " + rowIteratorCheck);
            LOG.info(":::::::::::; rowCheck = " + rowCheck);
            LOG.info(":::::::::::; cellIterator = " + cellIterator);
            int idx = 0;
            while (cellIterator.hasNext()) {
                cellIterator.next();
                idx++;
            }
            LOG.info(":::::::::::; idx = " + idx);
            if (totalColumn > idx) {
                return null;
            } else {
                if (hasHeader) {
                    if (rowIterator.hasNext()) {
                        rowIterator.next();
                    }
                }
                while (rowIterator.hasNext()) {
                    XSSFRow row = (XSSFRow) rowIterator.next();
                    LOG.info(":::::::::::; XSSFRow = " + row);
//                Iterator<Cell> cellIterator = row.cellIterator();
                    List<String> cellDataList = new ArrayList<String>();
//                while (cellIterator.hasNext()) {
                    if (row.getCell(0) == null) {
                        break;
                    }
                    for (int i = 0; i < totalColumn; i++) {
//                    XSSFCell cell = (XSSFCell) cellIterator.next();
                        XSSFCell cell = row.getCell(i);

                        LOG.info(":::::::::::; XSSFCell [" + i + "] = " + cell);
                        if (cell == null) {
                            cellDataList.add("");
                        } else {
                            cell.setCellType(XSSFCell.CELL_TYPE_STRING);
                            cellDataList.add(cell.getStringCellValue());
                        }
                    }
                    String[] cellArray = new String[totalColumn];
                    LOG.info(":::::::::::; cellArray = " + cellArray);
                    for (int i = 0; i < cellDataList.size(); i++) {
                        cellArray[i] = cellDataList.get(i);
                    }
                    dataList.add(cellArray);
                }
            }
        } catch (Exception ex) {
            throw ex;
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    throw e;
                }
            }
        }

        return dataList;
    }

    public static List<String[]> readCsvDataList(final InputStream inputStream, final String encoding, final String delimeter,
            final List<Tag> iedImpTempDelimitedList) throws Exception {
        List<String[]> dataList = new ArrayList<String[]>();
        BufferedReader br;
        String line;
        try {
            br = new BufferedReader(new InputStreamReader(inputStream, encoding));
            while ((line = br.readLine()) != null) {
                String[] dataTokens = line.split(delimeter, -1);
                String[] record;
                if (dataTokens.length != iedImpTempDelimitedList.size()) {
//                    throw new Exception("Data loss !!");
                    return null;
                } else {
                    record = new String[iedImpTempDelimitedList.size()];
                    System.arraycopy(dataTokens, 0, record, 0, record.length);
                    dataList.add(record);
                }
            }
        } catch (Exception ex) {
            throw ex;
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    throw e;
                }
            }
        }

        return dataList;
    }
}
